home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1995, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* trim.c
- * This program draws a rotating NURBS surface with a half-oval with
- * a triangular base trimmed from its center.
- *
- * Escape key - exit the program
- * <f> key - cycle through filled/wireframe/patch mode
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid initSurface( GLvoid );
- GLvoid drawSurface( GLUnurbsObj * );
- GLvoid cycleFillMode( GLvoid );
-
- void printNurbsError( GLenum );
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLfloat ctlpoints[4][4][3];
- static GLUnurbsObj *theNurb;
-
- static GLfloat angle = 0.0;
-
- void
- main(int argc, char *argv[])
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( 0, height/4 );
- glutInitWindowSize( width/2, height/2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - draw a rotating NURBS surface "
- "with a hole in the middle.\n\n"
- "Escape key - exit the program\n"
- "<f> key - cycle filled/wireframe/patch mode\n\n",
- progname);
-
- fprintf( stdout, "fillMode is GLU_OUTLINE_POLYGON\n" );
- }
-
- /* Initialize lighting, etc. */
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat mat_diffuse[] = { 0.8, 0.8, 0.2, 1.0 };
- GLfloat mat_specular[] = { 0.8, 0.0, 0.8, 1.0 };
- GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat mat_shininess[] = { 30.0 };
-
- GLfloat light_position[] = { 1.0, 0.0, 1.0, 0.0 };
-
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glClearColor( 0, 0, 0, 1 );
- glEnable( GL_DEPTH_TEST );
-
- glEnable( GL_AUTO_NORMAL );
- initSurface();
- theNurb = gluNewNurbsRenderer();
-
- /* Set program to display the outlines of the polygons
- * use to create the NURBS surface */
- gluNurbsProperty( theNurb, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON );
-
- /* Set up a callback function for error messages */
- gluNurbsCallback( theNurb, GLU_ERROR, printNurbsError );
- }
-
- GLvoid
- cycleFillMode( GLvoid )
- {
- static GLuint fillMode = GLU_OUTLINE_POLYGON;
-
- switch (fillMode) {
- case GLU_OUTLINE_POLYGON:
- fillMode = GLU_FILL;
- printf("fillMode is GLU_FILL\n");
- break;
- case GLU_FILL:
- fillMode = GLU_OUTLINE_PATCH;
- printf("fillMode is GLU_OUTLINE_PATCH\n");
- break;
- case GLU_OUTLINE_PATCH:
- fillMode = GLU_OUTLINE_POLYGON;
- printf("fillMode is GLU_OUTLINE_POLYGON\n");
- break;
- }
-
- /* Change the fill mode */
- gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, fillMode);
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'f': /* cycle fill mode */
- cycleFillMode();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- /* Initializes the control points of the surface to a small hill.
- * The control points range from -3 to +3 in x, y, and z
- */
- GLvoid
- initSurface(void)
- {
- int s, t;
-
- for (s = 0; s < 4; s++) {
- for (t = 0; t < 4; t++) {
- ctlpoints[s][t][0] = 2.0*((GLfloat)s - 1.5);
- ctlpoints[s][t][1] = 2.0*((GLfloat)t - 1.5);
-
- if ( (s == 1 || s == 2) && (t == 1 || t == 2))
- ctlpoints[s][t][2] = 3.0;
- else
- ctlpoints[s][t][2] = -3.0;
- }
- }
- }
-
- /* Draw a NURBS surface with a half-oval cut out of it */
- GLvoid
- drawSurface( GLUnurbsObj *theNurb )
- {
- GLfloat knots[8] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
- GLfloat edgePt[5][2] = /* counter clockwise */
- {{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}, {0.0, 1.0}, {0.0, 0.0}};
-
- GLfloat curveKnots[8] =
- {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
- GLfloat curvePts[4][2] = /* clockwise */
- {{0.1, 0.5}, {0.1, 0.9}, {0.9, 0.9}, {0.9, 0.5}};
- GLfloat pwlPts[4][2] = /* clockwise */
- {{0.9, 0.5}, {0.5, 0.1}, {0.1, 0.5}};
-
- gluBeginSurface( theNurb );
- gluNurbsSurface( theNurb,
- 8, knots,
- 8, knots,
- 4 * 3, 3,
- &ctlpoints[0][0][0],
- 4, 4,
- GL_MAP2_VERTEX_3);
-
- /* counter-clockwise trimming curve outlining the surface */
- gluBeginTrim( theNurb );
- gluPwlCurve( theNurb, 5, &edgePt[0][0], 2,
- GLU_MAP1_TRIM_2 );
- gluEndTrim( theNurb );
-
- /* clockwise trimming curves for the hole */
- gluBeginTrim( theNurb );
- gluNurbsCurve( theNurb, 8, curveKnots, 2,
- &curvePts[0][0], 4, GLU_MAP1_TRIM_2 );
- gluPwlCurve( theNurb, 3, &pwlPts[0][0], 2,
- GLU_MAP1_TRIM_2 );
- gluEndTrim( theNurb );
- gluEndSurface( theNurb );
- }
-
- /* Use the NURBS error number passed in to get a meaningful
- * string describing the error.
- */
- void
- printNurbsError( GLenum errnum )
- {
- printf("NURBS error: %s\n", gluErrorString( errnum ) );
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 3.0, 20.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -15.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- angle = fmodf( (angle + 5.0), 360.0 ); /* update angle */
- glutPostRedisplay(); /* Tell GLUT to redraw */
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();
- glRotatef( angle, 0.0, 1.0, 0.0 );
-
- drawSurface( theNurb );
-
- glPopMatrix();
-
- glutSwapBuffers();
- }
-
-